package com.hanhanxiaochong.zhongxiangchuanmei.widget;

import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.webview.WebActivity;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by rockphoenix on 2018/7/17.
 */

public class HomePageDialog extends DialogFragment {

    @BindView(R.id.iv_ad)
    ImageView ivAd;
    @BindView(R.id.iv_close)
    ImageView ivClose;
    private Unbinder unbinder;

    public static HomePageDialog newInstance(String picUrl, String linkUrl) {
        Bundle bundle = new Bundle();
        bundle.putString("picUrl", picUrl);
        bundle.putString("linkUrl", linkUrl);
        HomePageDialog homePageDialog = new HomePageDialog();
        homePageDialog.setArguments(bundle);
        return homePageDialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        //去掉dialog的标题，需要在setContentView()之前
        this.getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        Window window = this.getDialog().getWindow();
        //去掉dialog默认的padding
        assert window != null;
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (dm.widthPixels * 0.8f);
        lp.height = (int) (dm.heightPixels * 0.8f);
        window.setAttributes(lp);
        window.setBackgroundDrawable(new ColorDrawable());
        View dialog = ResourcesUtils.inflate(R.layout.dialog_homepage);
        Bundle arguments = getArguments();
        String picUrl = arguments.getString("picUrl");
        String linkUrl = arguments.getString("linkUrl");
        unbinder = ButterKnife.bind(this, dialog);
        GlideImgManager.glideLoader(getActivity(), RetrofitSource.image_url + picUrl, R.mipmap.img_error, R.mipmap.img_error, ivAd);
        ivAd.setOnClickListener(v -> {
            startActivity(WebActivity.createIntent(getActivity(), "活动", linkUrl, null));
            dismiss();
        });
        ivClose.setOnClickListener(v -> dismiss());
        return dialog;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
